package com.gxa.service.impl;

import com.gxa.mapper.*;
import com.gxa.pojo.*;
import com.gxa.service.IooService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * (Ioo)表服务实现类
 *
 * @author makejava
 * @since 2020-10-29 09:11:12
 */
@Service
public class IooServiceImpl implements IooService {
    @Autowired(required = false)
    private IooMapper iooMapper;
    @Autowired(required = false)
    private GoodsMapper goodsMapper;
    @Autowired(required = false)
    private CoditAttribMapper coditAttribMapper;
    @Autowired(required = false)
    private CoditInforMapper coditInforMapper;
    @Autowired(required = false)
    private CoditPageMapper coditPageMapper;
    @Autowired(required = false)
    private CostPriceMapper costPriceMapper;
    @Autowired(required = false)
    private CostSellingMapper costSellingMapper;
    @Autowired(required = false)
    private SupplierzMapper supplierzMapper;
    @Autowired(required = false)
    private OrderMapper orderMapper;
    /**
     * 通过ID查询单条数据
     *
     * @param iooId 主键
     * @return 实例对象
     */
    public Ioo queryById(Integer iooId) {
        return this.iooMapper.queryById(iooId);
    }


    /**
     * 修改库存单
     * @param ioo
     * @return
     */
    @Override
    public int updateIoo(Ioo ioo) {
        for (Goods goods :ioo.getGoodsList()) {
            CoditAttrib coditAttrib = goods.getCoditAttrib();
            CoditInfor coditInfor = goods.getCoditInfor();
            CostPrice costPrice = goods.getCostPrice();
            CostSelling costSelling = goods.getCostSelling();
            CoditPage coditPage = goods.getCoditPage();
            coditAttrib.setGoodsId(goods.getGoodsId());
            coditInfor.setGoodsId(goods.getGoodsId());
            costPrice.setGoodsId(goods.getGoodsId());
            costSelling.setGoodsId(goods.getGoodsId());
            coditPage.setGoodsId(goods.getGoodsId());
            coditAttribMapper.update(coditAttrib);
            coditInforMapper.update(coditInfor);
            costPriceMapper.update(costPrice);
            costSellingMapper.update(costSelling);
            coditPageMapper.update(coditPage);
            goodsMapper.update(goods);
        }
        return iooMapper.update(ioo);
    }
    /**
     * 模糊查询库存单
     * @param ioo
     * @return
     */
    @Override
    public List<Ioo> queryBlurryIoo(Ioo ioo) {
        return iooMapper.queryBlurryIoo(ioo);
    }
    /**
     * 查询所有库存单
     * @return
     */
    @Override
    public List<Ioo> queryIoo() {
        int totalSize=iooMapper.totalSizeIoo();
        if(totalSize!=0){
            List<Ioo> iooList = iooMapper.queryAll();
            for (Ioo ioo : iooList) {
                ioo.setGoodsList(goodsMapper.queryGoodsByIooId(ioo.getIooId()));
                if (ioo.getGoodsList()!=null){
                    for (Goods goods : ioo.getGoodsList()) {
                        double goodsQuanity=goodsMapper.selectGoodsQuanityByGoodsId(goods.getGoodsId(),ioo.getIooId());
                        goods.setCoditAttrib(coditAttribMapper.queryById(goods.getCoditAttribId()));
                        CoditInfor coditInfor=coditInforMapper.queryById(goods.getCoditInforId());
                        coditInfor.setSupplierz(supplierzMapper.queryById(coditInfor.getSuppliersId()));
                        goods.setCoditInfor(coditInfor);
                        goods.setCoditPage(coditPageMapper.queryById(goods.getCoditPageId()));
                        CostPrice costPrice = costPriceMapper.queryById(goods.getCostPriceId());
                        costPrice.setQuantity(goodsQuanity);
                        goods.setCostPrice(costPrice);
                        goods.setCostSelling(costSellingMapper.queryById(goods.getCostPriceGoodId()));
                    }
                }
                ioo.setOrder(orderMapper.queryById(ioo.getOrderId()));
            }
            return iooList;
        }else {
            return null;
        }
    }
    /**
     * 根据库存单类型查询库存单
     * @param typeState
     * @return
     */
    @Override
    public List<Ioo> queryIooByTypeState(int typeState) {
        int totalSize=iooMapper.totalSizeIoo();
        if(totalSize!=0){
            List<Ioo> iooList = iooMapper.queryAllByTypeState(typeState);
            for (Ioo ioo : iooList) {
                System.out.println("goodsList="+goodsMapper.queryGoodsByIooId(ioo.getIooId()));
                ioo.setGoodsList(goodsMapper.queryGoodsByIooId(ioo.getIooId()));
                System.out.println("goodsList="+ioo.getGoodsList());
                if (ioo.getGoodsList()!=null){
                    for (Goods goods : ioo.getGoodsList()) {
                        System.out.println("=================开始查数量=========================");
                        double goodsQuanity=goodsMapper.selectGoodsQuanityByGoodsId(goods.getGoodsId(),ioo.getIooId());
                        System.out.println("=================查完数量=========================");
                        System.out.println("数量="+goodsQuanity);
                        goods.setCoditAttrib(coditAttribMapper.queryById(goods.getCoditAttribId()));
                        CoditInfor coditInfor=coditInforMapper.queryById(goods.getCoditInforId());
                        coditInfor.setSupplierz(supplierzMapper.queryById(coditInfor.getSuppliersId()));
                        goods.setCoditInfor(coditInfor);
                        goods.setCoditPage(coditPageMapper.queryById(goods.getCoditPageId()));
                        CostPrice costPrice = costPriceMapper.queryById(goods.getCostPriceId());
                        costPrice.setQuantity(goodsQuanity);
                        goods.setCostPrice(costPrice);
                        goods.setCostSelling(costSellingMapper.queryById(goods.getCostPriceGoodId()));
                    }
                }
                ioo.setOrder(orderMapper.queryById(ioo.getOrderId()));
            }
            return iooList;
        }else {
            return null;
        }
    }
    /**
     * 根据库存单审批状态查询库存单
     * @param iooState
     * @return
     */
    @Override
    public List<Ioo> queryIooByIooState(int iooState) {
        List<Ioo> iooList = iooMapper.queryIooByIooState(iooState);
        if (iooList!=null){
            for (Ioo ioo : iooList) {
                ioo.setGoodsList(goodsMapper.queryGoodsByIooId(ioo.getIooId()));
                if (ioo.getGoodsList()!=null){
                    for (Goods goods : ioo.getGoodsList()) {
                        double goodsQuanity=goodsMapper.selectGoodsQuanityByGoodsId(goods.getGoodsId(),ioo.getIooId());
                        goods.setCoditAttrib(coditAttribMapper.queryById(goods.getCoditAttribId()));
                        CoditInfor coditInfor=coditInforMapper.queryById(goods.getCoditInforId());
                        coditInfor.setSupplierz(supplierzMapper.queryById(coditInfor.getSuppliersId()));
                        goods.setCoditInfor(coditInfor);
                        goods.setCoditPage(coditPageMapper.queryById(goods.getCoditPageId()));
                        CostPrice costPrice = costPriceMapper.queryById(goods.getCostPriceId());
                        costPrice.setQuantity(goodsQuanity);
                        goods.setCostPrice(costPrice);
                        goods.setCostSelling(costSellingMapper.queryById(goods.getCostPriceGoodId()));
                    }
                }
                ioo.setOrder(orderMapper.queryById(ioo.getOrderId()));
            }
            return iooList;
        }
        return null;
    }
    /**
     * 根据单据编码查询库存单
     * @param bill
     * @return
     */
    @Override
    public Ioo queryIooByBill(String bill) {
        Ioo ioo = iooMapper.queryIooByBill(bill);
        if (ioo!=null){
            ioo.setGoodsList(goodsMapper.queryGoodsByIooId(ioo.getIooId()));
            if (ioo.getGoodsList()!=null){
                for (Goods goods : ioo.getGoodsList()) {
                    double goodsQuanity=goodsMapper.selectGoodsQuanityByGoodsId(goods.getGoodsId(),ioo.getIooId());
                    goods.setCoditAttrib(coditAttribMapper.queryById(goods.getCoditAttribId()));
                    CoditInfor coditInfor=coditInforMapper.queryById(goods.getCoditInforId());
                    coditInfor.setSupplierz(supplierzMapper.queryById(coditInfor.getSuppliersId()));
                    goods.setCoditInfor(coditInfor);
                    goods.setCoditPage(coditPageMapper.queryById(goods.getCoditPageId()));
                    CostPrice costPrice = costPriceMapper.queryById(goods.getCostPriceId());
                    costPrice.setQuantity(goodsQuanity);
                    goods.setCostPrice(costPrice);
                    goods.setCostSelling(costSellingMapper.queryById(goods.getCostPriceGoodId()));
                }
            }
            ioo.setOrder(orderMapper.queryById(ioo.getOrderId()));
            return ioo;
        }
        return null;
    }
    /**
     * 添加库存单操作
     * @param ioos
     * @return
     */
    @Override
    public int addIoos(List<Ioo> ioos) throws ClassCastException {
        List<Goods> goodsList=null;
        int result=0;
        for (Ioo ioo : ioos) {
            int rows = iooMapper.addIoo(ioo);
            System.out.println("影响条数："+rows);
            Integer iooId = ioo.getIooId();
            System.out.println("iooId="+iooId);
            int branchCode = iooMapper.selectBranchCodeByWar_branch_name(ioo.getWarBranchName());
            System.out.println("branchCode="+branchCode);
            if (ioo.getTypeState() == 1) {//入库单
                goodsList = ioo.getGoodsList();
                for (Goods goods : goodsList) {
                    CoditInfor coditInfor = coditInforMapper.selectCoditInforByGoodsCode(goods.getCoditInfor().getGoodsCode());
                    System.out.println("coditInfor="+coditInfor);
                    if (coditInfor != null) {
                        Goods goods1 = goodsMapper.queryById(coditInfor.getGoodsId());
                        System.out.println("goods1="+goods1);
                        Integer goodsId = goods1.getGoodsId();
                        System.out.println("goodsId="+goodsId);
                        CostPrice costPrice = costPriceMapper.queryById(goods1.getCostPriceId());
                        System.out.println("costPrice="+costPrice);
                        Double quantity1 = costPrice.getQuantity();
                        System.out.println("quantity1="+quantity1);
                        double quantity = 0;
                        quantity=quantity1 + goods.getCostPrice().getQuantity();
                        System.out.println("quantity="+quantity);
                        iooMapper.andGoodsIoo(iooId, goodsId);
                        result += costPriceMapper.updateCostPrice(goods1.getCostPriceId(), quantity);
                        Map<String,Object> map=iooMapper.selectGoodsIoo(goodsId,iooId);
                        if (map!=null){
                            result=iooMapper.updateGoodsIoo(Integer.parseInt(map.get("goods_ioo_id").toString()),goods.getCostPrice().getQuantity());
                        }else {
                            result=iooMapper.addGoodsIoo(goodsId,goods.getCostPrice().getQuantity(),iooId);
                        }
                    } else {
                        goods.setBranchCode(branchCode);
                        goods.setIooId(iooId);
                        System.out.println("goods="+goods);
                        int result1 = goodsMapper.addGoods(goods);
                        Integer goodsId = goods.getGoodsId();
                        System.out.println("goodsId="+goodsId+"iooId="+iooId);
                        iooMapper.andGoodsIoo(iooId, goodsId);
                        CoditAttrib coditAttrib=goods.getCoditAttrib();
                        CoditPage coditPage=goods.getCoditPage();
                        CostPrice costPrice=goods.getCostPrice();
                        CoditInfor coditInfor1=goods.getCoditInfor();
                        CostSelling costSelling=goods.getCostSelling();
                        if (coditAttrib!=null){
                            coditAttrib.setGoodsId(goodsId);
                            int rows0 = coditAttribMapper.addCoditAttrib(coditAttrib);
                            Integer coditAttribId = coditAttrib.getCoditAttribId();
                            goods.setCoditAttribId(coditAttribId);
                        }
                        if (coditPage!=null){
                            coditPage.setGoodsId(goodsId);
                            int rows1 = coditPageMapper.addCoditPage(coditPage);
                            Integer coditPageId = coditPage.getCoditPageId();
                            goods.setCoditPageId(coditPageId);
                        }
                        if (costPrice!=null){
                            costPrice.setGoodsId(goodsId);
                            int rows2 = costPriceMapper.addCostPrice(costPrice);
                            Integer costPriceId = costPrice.getCostPriceId();
                            goods.setCostPriceId(costPriceId);
                        }
                        if (coditInfor1!=null){
                            coditInfor1.setGoodsId(goodsId);
                            int rows3 = coditInforMapper.addCoditInfor(coditInfor1);
                            Integer coditInforId = coditInfor1.getCoditInforId();
                            goods.setCoditInforId(coditInforId);
                        }
                        if (costSelling!=null){
                            costSelling.setGoodsId(goodsId);
                            int rows4 = costSellingMapper.addCostSelling(costSelling);
                            Integer costPriceGoodId = costSelling.getCostPriceGoodId();
                            goods.setCostPriceGoodId(costPriceGoodId);
                        }
                        goods.setIooId(iooId);
                        result += goodsMapper.update(goods);
                        Map<String,Object> map=iooMapper.selectGoodsIoo(goodsId,iooId);
                        if (map!=null){
                            result=iooMapper.updateGoodsIoo(Integer.parseInt(map.get("goods_ioo_id").toString()),goods.getCostPrice().getQuantity());
                        }else {
                            result=iooMapper.addGoodsIoo(goodsId,goods.getCostPrice().getQuantity(),iooId);
                        }
                    }
                }
            }
            if (ioo.getTypeState() == 2) {//出库单
                goodsList = ioo.getGoodsList();
                for (Goods goods : goodsList) {
                    CoditInfor coditInfor = coditInforMapper.selectCoditInforByGoodsCode(goods.getCoditInfor().getGoodsCode());
                    if (coditInfor != null) {
                        Goods goods1 = goodsMapper.queryById(coditInfor.getGoodsId());
                        Integer goodsId = goods1.getGoodsId();
                        CostPrice costPrice = costPriceMapper.queryById(goods1.getCostPriceId());
                        Double quantity = costPrice.getQuantity();
                        quantity = quantity - goods.getCostPrice().getQuantity();
                        iooMapper.andGoodsIoo(iooId, goods1.getGoodsId());
                        result += costPriceMapper.updateCostPrice(goods1.getCostPriceId(), quantity);
                        Map<String,Object> map=iooMapper.selectGoodsIoo(goodsId,iooId);
                        if (map!=null){
                            result=iooMapper.updateGoodsIoo(Integer.parseInt(map.get("goods_ioo_id").toString()),goods.getCostPrice().getQuantity());
                        }else {
                            result=iooMapper.addGoodsIoo(goodsId,goods.getCostPrice().getQuantity(),iooId);
                        }
                    } else {
                        throw new ClassCastException();
                    }
                }
            }
            if (ioo.getTypeState() == 3) {//门店调拨单
                goodsList = ioo.getGoodsList();
                System.out.println("goodsList="+goodsList);
                for (Goods goods : goodsList) {
                    System.out.println("GoodsCode="+goods.getCoditInfor().getGoodsCode());
                    CoditInfor coditInfor = coditInforMapper.selectCoditInforByGoodsCode(goods.getCoditInfor().getGoodsCode());
                    System.out.println("coditInfor="+coditInfor);
                    if (coditInfor == null) {
                        throw new ClassCastException();
                    }else {
                        Goods goods1 = goodsMapper.queryById(coditInfor.getGoodsId());
                        Integer goodsId = goods1.getGoodsId();
                        Map<String,Object> map=iooMapper.selectGoodsIoo(goodsId,iooId);
                        if (map!=null){
                            result=iooMapper.updateGoodsIoo(Integer.parseInt(map.get("goods_ioo_id").toString()),goods.getCostPrice().getQuantity());
                        }else {
                            result=iooMapper.addGoodsIoo(goodsId,goods.getCostPrice().getQuantity(),iooId);
                        }
                    }
                }
            }
            if (ioo.getTypeState() == 4) {//门店退货单
                goodsList = ioo.getGoodsList();
                for (Goods goods : goodsList) {

                    CoditInfor coditInfor = coditInforMapper.selectCoditInforByGoodsCode(goods.getCoditInfor().getGoodsCode());
                    if (coditInfor != null) {
                        Goods goods1 = goodsMapper.queryById(coditInfor.getGoodsId());
                        CostPrice costPrice = costPriceMapper.queryById(goods1.getCostPriceId());
                        Double quantity = costPrice.getQuantity();
                        quantity = quantity + goods.getCostPrice().getQuantity();
                        iooMapper.andGoodsIoo(iooId, goods1.getGoodsId());
                        result += costPriceMapper.updateCostPrice(goods1.getCostPriceId(), quantity);
                        Integer goodsId = goods1.getGoodsId();
                        Map<String,Object> map=iooMapper.selectGoodsIoo(goodsId,iooId);
                        if (map!=null){
                            result=iooMapper.updateGoodsIoo(Integer.parseInt(map.get("goods_ioo_id").toString()),goods.getCostPrice().getQuantity());
                        }else {
                            result=iooMapper.addGoodsIoo(goodsId,goods.getCostPrice().getQuantity(),iooId);
                        }
                    } else {
                        throw new ClassCastException();
                    }
                }
            }
            if (ioo.getTypeState() == 5) {//采购退货单
                goodsList = ioo.getGoodsList();
                for (Goods goods : goodsList) {
                    CoditInfor coditInfor = coditInforMapper.selectCoditInforByGoodsCode(goods.getCoditInfor().getGoodsCode());
                    if (coditInfor != null) {
                        Goods goods1 = goodsMapper.queryById(coditInfor.getGoodsId());
                        Integer goodsId = goods1.getGoodsId();
                        CostPrice costPrice = costPriceMapper.queryById(goods1.getCostPriceId());
                        Double quantity = costPrice.getQuantity();
                        quantity = quantity - goods.getCostPrice().getQuantity();
                        if (quantity < 0) {
                            throw new ClassCastException();
                        }
                        iooMapper.andGoodsIoo(iooId, goods1.getGoodsId());
                        result += costPriceMapper.updateCostPrice(goods1.getCostPriceId(), quantity);
                        Map<String,Object> map=iooMapper.selectGoodsIoo(goodsId,iooId);
                        if (map!=null){
                            result=iooMapper.updateGoodsIoo(Integer.parseInt(map.get("goods_ioo_id").toString()),goods.getCostPrice().getQuantity());
                        }else {
                            result=iooMapper.addGoodsIoo(goodsId,goods.getCostPrice().getQuantity(),iooId);
                        }
                    } else {
                        throw new ClassCastException();
                    }
                }
            }
        }
        return result;
    }

    @Override
    public int addIoo(Ioo ioo) throws ClassCastException {
        List<Goods> goodsList=null;
        int result=0;
        int rows = iooMapper.addIoo(ioo);
        System.out.println("影响条数："+rows);
        Integer iooId = ioo.getIooId();
        System.out.println("iooId="+iooId);
        int branchCode = iooMapper.selectBranchCodeByWar_branch_name(ioo.getWarBranchName());
        System.out.println("branchCode="+branchCode);
        if (ioo.getTypeState() == 1) {//入库单
            goodsList = ioo.getGoodsList();
            for (Goods goods : goodsList) {
                CoditInfor coditInfor = coditInforMapper.selectCoditInforByGoodsCode(goods.getCoditInfor().getGoodsCode());
                System.out.println("coditInfor="+coditInfor);
                if (coditInfor != null) {
                    Goods goods1 = goodsMapper.queryById(coditInfor.getGoodsId());
                    System.out.println("goods1="+goods1);
                    Integer goodsId = goods1.getGoodsId();
                    System.out.println("goodsId="+goodsId);
                    CostPrice costPrice = costPriceMapper.queryById(goods1.getCostPriceId());
                    System.out.println("costPrice="+costPrice);
                    Double quantity1 = costPrice.getQuantity();
                    System.out.println("quantity1="+quantity1);
                    double quantity = 0;
                    quantity=quantity1 + goods.getCostPrice().getQuantity();
                    System.out.println("quantity="+quantity);
                    iooMapper.andGoodsIoo(iooId, goodsId);
                    result += costPriceMapper.updateCostPrice(goods1.getCostPriceId(), quantity);
                    Map<String,Object> map=iooMapper.selectGoodsIoo(goodsId,iooId);
                    if (map!=null){
                        result=iooMapper.updateGoodsIoo(Integer.parseInt(map.get("goods_ioo_id").toString()),goods.getCostPrice().getQuantity());
                    }else {
                        result=iooMapper.addGoodsIoo(goodsId,goods.getCostPrice().getQuantity(),iooId);
                    }
                } else {
                    goods.setBranchCode(branchCode);
                    goods.setIooId(iooId);
                    System.out.println("goods="+goods);
                    int result1 = goodsMapper.addGoods(goods);
                    Integer goodsId = goods.getGoodsId();
                    System.out.println("goodsId="+goodsId+"iooId="+iooId);
                    iooMapper.andGoodsIoo(iooId, goodsId);
                    CoditAttrib coditAttrib=goods.getCoditAttrib();
                    CoditPage coditPage=goods.getCoditPage();
                    CostPrice costPrice=goods.getCostPrice();
                    CoditInfor coditInfor1=goods.getCoditInfor();
                    CostSelling costSelling=goods.getCostSelling();
                    if (coditAttrib!=null){
                        coditAttrib.setGoodsId(goodsId);
                        int rows0 = coditAttribMapper.addCoditAttrib(coditAttrib);
                        Integer coditAttribId = coditAttrib.getCoditAttribId();
                        goods.setCoditAttribId(coditAttribId);
                    }
                    if (coditPage!=null){
                        coditPage.setGoodsId(goodsId);
                        int rows1 = coditPageMapper.addCoditPage(coditPage);
                        Integer coditPageId = coditPage.getCoditPageId();
                        goods.setCoditPageId(coditPageId);
                    }
                    if (costPrice!=null){
                        costPrice.setGoodsId(goodsId);
                        int rows2 = costPriceMapper.addCostPrice(costPrice);
                        Integer costPriceId = costPrice.getCostPriceId();
                        goods.setCostPriceId(costPriceId);
                    }
                    if (coditInfor1!=null){
                        coditInfor1.setGoodsId(goodsId);
                        int rows3 = coditInforMapper.addCoditInfor(coditInfor1);
                        Integer coditInforId = coditInfor1.getCoditInforId();
                        goods.setCoditInforId(coditInforId);
                    }
                    if (costSelling!=null){
                        costSelling.setGoodsId(goodsId);
                        int rows4 = costSellingMapper.addCostSelling(costSelling);
                        Integer costPriceGoodId = costSelling.getCostPriceGoodId();
                        goods.setCostPriceGoodId(costPriceGoodId);
                    }
                    goods.setIooId(iooId);
                    result += goodsMapper.update(goods);
                    Map<String,Object> map=iooMapper.selectGoodsIoo(goodsId,iooId);
                    if (map!=null){
                        result=iooMapper.updateGoodsIoo(Integer.parseInt(map.get("goods_ioo_id").toString()),goods.getCostPrice().getQuantity());
                    }else {
                        result=iooMapper.addGoodsIoo(goodsId,goods.getCostPrice().getQuantity(),iooId);
                    }
                }
            }
        }
        if (ioo.getTypeState() == 2) {//出库单
            goodsList = ioo.getGoodsList();
            for (Goods goods : goodsList) {
                CoditInfor coditInfor = coditInforMapper.selectCoditInforByGoodsCode(goods.getCoditInfor().getGoodsCode());
                if (coditInfor != null) {
                    Goods goods1 = goodsMapper.queryById(coditInfor.getGoodsId());
                    Integer goodsId = goods1.getGoodsId();
                    CostPrice costPrice = costPriceMapper.queryById(goods1.getCostPriceId());
                    Double quantity = costPrice.getQuantity();
                    quantity = quantity - goods.getCostPrice().getQuantity();
                    iooMapper.andGoodsIoo(iooId, goods1.getGoodsId());
                    result += costPriceMapper.updateCostPrice(goods1.getCostPriceId(), quantity);
                    Map<String,Object> map=iooMapper.selectGoodsIoo(goodsId,iooId);
                    if (map!=null){
                        result=iooMapper.updateGoodsIoo(Integer.parseInt(map.get("goods_ioo_id").toString()),goods.getCostPrice().getQuantity());
                    }else {
                        result=iooMapper.addGoodsIoo(goodsId,goods.getCostPrice().getQuantity(),iooId);
                    }
                } else {
                    throw new ClassCastException();
                }
            }
        }
        if (ioo.getTypeState() == 3) {//门店调拨单
            goodsList = ioo.getGoodsList();
            System.out.println("goodsList="+goodsList);
            for (Goods goods : goodsList) {
                System.out.println("GoodsCode="+goods.getCoditInfor().getGoodsCode());
                CoditInfor coditInfor = coditInforMapper.selectCoditInforByGoodsCode(goods.getCoditInfor().getGoodsCode());
                System.out.println("coditInfor="+coditInfor);
                if (coditInfor == null) {
                    throw new ClassCastException();
                }else {
                    Goods goods1 = goodsMapper.queryById(coditInfor.getGoodsId());
                    Integer goodsId = goods1.getGoodsId();
                    Map<String,Object> map=iooMapper.selectGoodsIoo(goodsId,iooId);
                    if (map!=null){
                        result=iooMapper.updateGoodsIoo(Integer.parseInt(map.get("goods_ioo_id").toString()),goods.getCostPrice().getQuantity());
                    }else {
                        result=iooMapper.addGoodsIoo(goodsId,goods.getCostPrice().getQuantity(),iooId);
                    }
                }
            }
        }
        if (ioo.getTypeState() == 4) {//门店退货单
            goodsList = ioo.getGoodsList();
            for (Goods goods : goodsList) {

                CoditInfor coditInfor = coditInforMapper.selectCoditInforByGoodsCode(goods.getCoditInfor().getGoodsCode());
                if (coditInfor != null) {
                    Goods goods1 = goodsMapper.queryById(coditInfor.getGoodsId());
                    CostPrice costPrice = costPriceMapper.queryById(goods1.getCostPriceId());
                    Double quantity = costPrice.getQuantity();
                    quantity = quantity + goods.getCostPrice().getQuantity();
                    iooMapper.andGoodsIoo(iooId, goods1.getGoodsId());
                    result += costPriceMapper.updateCostPrice(goods1.getCostPriceId(), quantity);
                    Integer goodsId = goods1.getGoodsId();
                    Map<String,Object> map=iooMapper.selectGoodsIoo(goodsId,iooId);
                    if (map!=null){
                        result=iooMapper.updateGoodsIoo(Integer.parseInt(map.get("goods_ioo_id").toString()),goods.getCostPrice().getQuantity());
                    }else {
                        result=iooMapper.addGoodsIoo(goodsId,goods.getCostPrice().getQuantity(),iooId);
                    }
                } else {
                    throw new ClassCastException();
                }
            }
        }
        if (ioo.getTypeState() == 5) {//采购退货单
            goodsList = ioo.getGoodsList();
            for (Goods goods : goodsList) {
                CoditInfor coditInfor = coditInforMapper.selectCoditInforByGoodsCode(goods.getCoditInfor().getGoodsCode());
                if (coditInfor != null) {
                    Goods goods1 = goodsMapper.queryById(coditInfor.getGoodsId());
                    Integer goodsId = goods1.getGoodsId();
                    CostPrice costPrice = costPriceMapper.queryById(goods1.getCostPriceId());
                    Double quantity = costPrice.getQuantity();
                    quantity = quantity - goods.getCostPrice().getQuantity();
                    if (quantity < 0) {
                        throw new ClassCastException();
                    }
                    iooMapper.andGoodsIoo(iooId, goods1.getGoodsId());
                    result += costPriceMapper.updateCostPrice(goods1.getCostPriceId(), quantity);
                    Map<String,Object> map=iooMapper.selectGoodsIoo(goodsId,iooId);
                    if (map!=null){
                        result=iooMapper.updateGoodsIoo(Integer.parseInt(map.get("goods_ioo_id").toString()),goods.getCostPrice().getQuantity());
                    }else {
                        result=iooMapper.addGoodsIoo(goodsId,goods.getCostPrice().getQuantity(),iooId);
                    }
                } else {
                    throw new ClassCastException();
                }
            }
        }
        return result;
    }

    @Override
    public Ioo queryIooByIooid(int iooid) {
        Ioo ioo = iooMapper.queryIooByIooid(iooid);
        if (ioo!=null){
            ioo.setGoodsList(goodsMapper.queryGoodsByIooId(ioo.getIooId()));
            if (ioo.getGoodsList()!=null){
                for (Goods goods : ioo.getGoodsList()) {
                    System.out.println("goods="+goods);
                    double goodsQuanity=goodsMapper.selectGoodsQuanityByGoodsId(goods.getGoodsId(),ioo.getIooId());
                    goods.setCoditAttrib(coditAttribMapper.queryById(goods.getCoditAttribId()));
                    CoditInfor coditInfor=coditInforMapper.queryById(goods.getCoditInforId());
                    coditInfor.setSupplierz(supplierzMapper.queryById(coditInfor.getSuppliersId()));
                    goods.setCoditInfor(coditInfor);
                    goods.setCoditPage(coditPageMapper.queryById(goods.getCoditPageId()));
                    CostPrice costPrice = costPriceMapper.queryById(goods.getCostPriceId());
                    costPrice.setQuantity(goodsQuanity);
                    goods.setCostPrice(costPrice);
                    goods.setCostSelling(costSellingMapper.queryById(goods.getCostPriceGoodId()));
                }
            }
            ioo.setOrder(orderMapper.queryById(ioo.getOrderId()));
            return ioo;
        }
        return null;
    }

    @Override
    public int updateIooStateAndIooRfrByIooId(Ioo ioo) {
        return iooMapper.update(ioo);
    }

    /**
     * 通过主键删除数据
     *
     * @param iooId 主键
     * @return 是否成功
     */
    @Override
    public int deleteIooByIooId(int iooId) {
        return iooMapper.deleteIooByIooId(iooId);
    }
}